package com.project.nimsen.web.sys;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.jdbc.utils.DateUtils;
import com.project.jdbc.utils.Page;
import com.project.jdbc.utils.RegexValidateUtils;
import com.project.jdbc.web.BaseMVCAction;
import com.project.nimsen.common.web.LoginUser;
import com.project.nimsen.dto.MemberDto;
import com.project.nimsen.entity.TMerberEntity;
import com.project.nimsen.service.IMemberService;
import com.project.nimsen.util.WebUtil;
@Controller
@RequestMapping(value ="/sys/member")
public class MemberControl extends BaseMVCAction {
	@Resource
	IMemberService memberService;
	@Resource
	WebUtil webUtil;
	//跳转到手动添加人员信息页面
	@RequestMapping(value="/add")
	public String add(){
		return "WEB-INF/sys/member/add_mem";
	}
	//左则信息查询
	@RequestMapping(value="/leftSearch")
	@ResponseBody
	public List<Map<String, Object>> leftSearch(){
		try{
			MemberDto dto = new MemberDto();
			dto.setActive("2");
			List<Map<String, Object>> rows = memberService.findByDto(dto);
			if(CollectionUtils.isEmpty(rows))
				return null;
			for(Map<String, Object> row : rows){
				row.put("open", true);
			}
			return rows;
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}
	//审核会员信息
	@RequestMapping(value="/shuser")
	@ResponseBody
	public Map<String, Object> shuser(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		String ids = getParameter("ids");
		if(StringUtils.isBlank(ids)||",".equals(ids)){
			map.put("message", "必须选择一条会员信息");
			return map;
		}
		try{
			//MemberDto dto = new MemberDto();
			LoginUser loginUser = LoginUser.getCurr();
			if(memberService.updateSKByIds(ids, loginUser.getUserid())){
				map.put("success", true);
			}else{
				map.put("message", "审核会员失败");
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return map;
	}
	//查询信息
	@RequestMapping(value="/search")
	@ResponseBody
	public Map<String, Object> search(){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("success", false);
		map.put("total", 0);
		MemberDto dto = new MemberDto();
		Integer pageIndex = getIntegerParameter("page");
		if(pageIndex!=null){
			Page page = new Page(pageIndex);
			page.setPageSize(getIntegerParameter("rows"));
			dto.setPage(page);
		}
		String pid=getParameter("pid");
		if(StringUtils.isBlank(pid)){
			map.put("success", true);
			map.put("rows", "");
			map.put("total", 0);
			return map;
		}
		dto.setPid(pid);
		try {
			List<Map<String, Object>> rows = memberService.findByDto(dto);
			if(CollectionUtils.isEmpty(rows)){
				map.put("success", true);
				map.put("rows", "");
				map.put("total", 0);
				return map;
			}
			for(Map<String, Object> row : rows){
				Object obj_date = row.get("CREATE_TIME");
				if(obj_date!=null){
					Date time = (Date)obj_date;
					row.put("CREATE_TIME", DateUtils.convertDate2String(time));
				}
				Object obj_reg = row.get("REG_TIME");
				if(obj_reg!=null){
					Date time = (Date)obj_reg;
					row.put("REG_TIME", DateUtils.convertDate2String(time));
				}
				Object exa_reg = row.get("EXA_TIME");
				if(exa_reg!=null){
					Date time = (Date)obj_reg;
					row.put("EXA_TIME", DateUtils.convertDate2String(time));
				}
				Object obj_act = row.get("ACTIVE");
				if("2".equals(obj_act.toString())){
					row.put("ACTIVE", "已审核");
				}else if("0".equals(obj_act.toString())){
					row.put("ACTIVE", "未审核");
				}
				//推广连接
				row.put("URL", webUtil.webUrl(getRequest())+"/pub/mem/"+row.get("ID").toString());
			}
			map.put("success", true);
			map.put("rows", rows);
			map.put("total", dto.getPage().getTotalCount());
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return map;
	}
	//手动添加一个会员信息
	@RequestMapping(value="/dosave")
	@ResponseBody
	public Map<String, Object> dosave(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		String name = getParameter("name");
		if(StringUtils.isBlank(name)){
			map.put("message", "姓名不可以为空");
			return map;
		}
		/*String card_id = getParameter("card_id");
		if(!RegexValidateUtils.isCardid(card_id)){
			map.put("message", "身份证格式不正确");
			return map;
		}*/
		String weixin = getParameter("weixin");
		if(StringUtils.isBlank(weixin)){
			map.put("message", "微信号不可以为空");
			return map;
		}
		String mphone = getParameter("mphone");
		if(!RegexValidateUtils.isMphone(mphone)){
			map.put("message", "手机号格式不正确");
			return map;
		}
		/*String bank_card_id = getParameter("bank_card_id");
		if(StringUtils.isNotBlank(bank_card_id)){
			if(!RegexValidateUtils.isbankcardid(bank_card_id)){
				map.put("message", "银行卡号格式不正确");
				return map;
			}
		}*/
		//添加用户的外围信息
		MemberDto dto = new MemberDto();
		LoginUser loginUser = LoginUser.getCurr();
		dto.setLogin_user_id(loginUser.getUserid());
		//封装实体类
		TMerberEntity ent = null;
		String id = getParameter("id");
		try {
			if(StringUtils.isBlank(id)){
				ent = new TMerberEntity();
				ent.setACTIVE(2);
				ent.setCREATE_TIME(new Date());
				ent.setPID(getParameter("pid"));
			}else{
				ent = memberService.findByID(id);
			}
			//ent.setBANK_CARD_ID(bank_card_id);
			//ent.setCARD_ID(card_id);
			ent.setM_TRUE_NAME(name);
			ent.setMPHONE(mphone);
			ent.setWEIXIN(weixin);
			String message = memberService.findVal(ent);
			if(!"true".equals(message)){
				map.put("message", message);
				return map;
			}
			if(StringUtils.isBlank(id)){
				Object obj_id = memberService.saveEnt(ent, dto);
				if(obj_id!=null)
					map.put("success", true);
				else
					map.put("message", "保存失败");
			}else{
				if(memberService.update(ent)>0){
					map.put("success", true);
				}else{
					map.put("message", "修改失败");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		
		return map;
	}
}
